package com.naiterui.ehp.bs.pc.modules.security.utils;

import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RsaUtil {

    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9wo1C9WsufgJe7vG00K6+/V//rkROfKkhY2zvgx6Y75V5lXr5tUZeBwX0Euimu4PYg9n7hsShPyJHHlss/B+LmVV5ChpvlPEmf3VQHgvvTRdPhjHWExcx05+ROKOuon0J4BmUfRmDrI+/59u69B2QM1YHcF1RLzwJymBr2CzHmwIDAQAB";
    private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL3CjUL1ay5+Al7u8bTQrr79X/"
        + "+uRE58qSFjbO+DHpjvlXmVevm1Rl4HBfQS6Ka7g9iD2fuGxKE/IkceWyz8H4uZVXkKGm+U8SZ/dVAeC+9NF0+GMdYTFzHTn5E4o66i"
        + "fQngGZR9GYOsj7/n27r0HZAzVgdwXVEvPAnKYGvYLMebAgMBAAECgYEAqWYphxHhueJhqW/ztG/q37L/ZRekANEGhz"
        + "jkAbjysqKGtMB9xLdP9xab+PR8XoPdQocKLKF1nWDn78coq7LzULda/fStHs/TQlAySa0Pdts/JSnTp1NRK64TgbiLqwjRrfY+Je"
        + "9QhYtMiHoobDIjy9ktBknkGm+US03ZMSTUQZkCQQD0RE3niv//PqyBThLlttPyIaq7cHxONf/hfJV9gBixSh5LMcHoM"
        + "B4Sc6B4qef6wwsHM2eMlqeoZV7aqjtt8wU1AkEAxt/9G8gx++VE3HHWJ0Z/LMdJK2Jw8V+6IScVkJwkbh8yENDkpro/"
        + "2EwAq5ImK8iT5ZCKPBxfM9rMg8vPONBDjwJBANkEfnqC4mJx2m2fYea5gqCTDM8vZyFNlKNPPrmbv1R0V2nTMUYKygz2"
        + "sdHCkpoWA4xIPW2IxDg2qAl6SQasbSkCQEgjH1BggaD5PM0pjaCZda/saaIPByDGtGEqKCxYNu6q6iInY4kGrfnrN6ME"
        + "VEw1aVAv6bR8JhxNFToJR5U4FgkCQDT09A2pevWlDrpNBZHjcIDRvColfMogrtb2y1vVzMghAeq2arnO6HZ9vxERCVsp99iWFKOXxIVcEai6ZwZQvWE=";

    public static String encryptData(String data) {
        return RsaUtil.encryptData(data, PUBLIC_KEY);
    }

    /**
     * RSA加密
     */
    @SuppressWarnings("WeakerAccess")
    public static String encryptData(String data, String publicKey) {
        try {
            //base64编码的公钥
            byte[] decoded = Base64.decodeBase64(publicKey);
            RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));

            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            byte[] dataToEncrypt = data.getBytes(Charsets.UTF_8);
            byte[] encryptedData = cipher.doFinal(dataToEncrypt);
            return Base64.encodeBase64String(encryptedData);
        } catch (Exception e) {
            log.warn("RSA加密失败 data:{}", data, e);
        }
        return null;
    }

    public static String decryptData(String data) {
        return decryptData(data, PRIVATE_KEY);
    }

    /**
     * RSA解密
     */
    @SuppressWarnings("WeakerAccess")
    public static String decryptData(String data, String privateKey) {
        try {
            byte[] decoded = Base64.decodeBase64(privateKey);
            RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, priKey);
            byte[] descryptData = Base64.decodeBase64(data);
            byte[] descryptedData = cipher.doFinal(descryptData);
            return new String(descryptedData, Charsets.UTF_8);
        } catch (Exception e) {
            log.warn("RSA解密失败 data:{}", data, e);
        }
        return null;
    }

}